package com.wf.smartcomm.server.admin.action;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.json.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ModelDriven;
import com.wf.smartcomm.server.bean.House;
import com.wf.smartcomm.server.bean.UnitMachine;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.service.HouseService;
import com.wf.smartcomm.server.service.UnitMachService;
import com.wf.smartcomm.server.service.UserService;
import com.wf.smartcomm.server.util.ConstantUtils;
import com.wf.smartcomm.server.util.IdUtils;
import com.wf.smartcomm.server.util.StringUtil;

@Controller
@Scope("prototype")
public class UmAction extends BaseAction implements ModelDriven<UnitMachine>{

	private UnitMachine um = new UnitMachine();
	
	@Resource
	private UnitMachService umServiceImpl;
	
	@Resource
	private HouseService houseServiceImpl;
	
	@Resource
	private UserService userServiceImpl;
	
	public String add() throws Exception{
		User user = (User) session.getAttribute("loginUser");
		houseServiceImpl.listRegion(user.getLoginVillage(), page);
		request.setAttribute("regions", page.getDataList());
		return "add";
	}
	
	public void save() throws Exception{
		String hardwareId = um.getHardwareId();
		User user = (User) session.getAttribute("loginUser");
		um.setVillage(user.getLoginVillage());
		if(StringUtil.isEmpty(um.getObjectId())){
			if (umServiceImpl.getByHardId(hardwareId) != null) {
				printJsonData(202);
				return;
			}
			User umUser = new User();
			umUser.setType(ConstantUtils.USER_TYPE_UM);
			umUser.setLoginVillage(user.getLoginVillage());
			umUser.setObjectId(IdUtils.uuid());
			umUser.setPushId(IdUtils.uuid16());
			umUser.setCreatedAt(String.valueOf(System.currentTimeMillis()));
			umUser.setUpdateAt(umUser.getCreatedAt());
			umUser.setIsUnReg("0");
			umUser.setDeviceType(ConstantUtils.DEVICE_TYPE_UNKNOW);
			userServiceImpl.registe(umUser);
			um.setPushId(umUser.getPushId());
			um.setUser(umUser);
			um.setTimeSpan("300000");
			umServiceImpl.save(um);
			if (um.getType() == ConstantUtils.UM_TYPE_REGION) {
				printJsonData(200);
				return;
			}else{
				 houseServiceImpl.listBySelect(user.getLoginVillage(), um.getRegion(), um.getBuilding(), um.getEntrance(), page);
				 List<House> houses = (List<House>) page.getDataList();
				 umServiceImpl.saveHouseRelation(this.um, houses);
			}
			
		}else {
			UnitMachine sourceUm = umServiceImpl.getById(um.getObjectId());
			if (sourceUm == null) {
				printJsonData(401);
				return;
			}
			um.setVoipNumber(sourceUm.getVoipNumber());
			um.setUser(sourceUm.getUser());
			umServiceImpl.update(um);
		}
     
		printJsonData(200);
	}
	
	public void list() throws Exception {
		User user = (User) session.getAttribute("loginUser");
		String aoData = request.getParameter("aoData");
		String sEcho = "";
		String iDisplayStart = "";
		String iDisplayLength = "";
		String sSearch = "";
		
		JSONObject jo = new JSONObject(aoData);
		sEcho = jo.getString("sEcho");
		iDisplayStart = jo.getString("iDisplayStart");
		iDisplayLength = jo.getString("iDisplayLength");
		sSearch = jo.getString("sSearch");
		
		page.setBeginIndex(Integer.parseInt(iDisplayStart));
		page.setPageSize(Integer.parseInt(iDisplayLength));
		if (!StringUtil.isEmpty(sSearch)) {
			umServiceImpl.queryByKey(user.getLoginVillage(),sSearch,page);
		}else {
			umServiceImpl.listByVillage(user.getLoginVillage(), page);
		}
		
		int  initEcho = Integer.parseInt(sEcho)+1;
		int count = page.getTotalCount();
		HashMap<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("iTotalRecords", count);
		dataMap.put("sEcho",initEcho);
		dataMap.put("iTotalDisplayRecords", count);
		dataMap.put("aaData", page.getDataList());
		printJsonData(outGson.toJson(dataMap));
	}
	
	public void offLine(){
		umServiceImpl.offLine(um);
	}
	
	public String edit() throws Exception{
		User user = (User) session.getAttribute("loginUser");
		houseServiceImpl.listRegion(user.getLoginVillage(), page);
		request.setAttribute("regions", page.getDataList());
		um = umServiceImpl.getById(um.getObjectId());
		request.setAttribute("um", um);
		return "edit";
	}
	
	public void delete() throws Exception{
		um = umServiceImpl.getById(um.getObjectId());
		User user = um.getUser();
		umServiceImpl.delete(um);
		userServiceImpl.deleteUmUser(user);
		printJsonData(200);
	}
	
	public String showUnlock() throws Exception{
		um = umServiceImpl.getById(um.getObjectId());
		request.setAttribute("um", um);
		return "unlock";
	}
	
	
	@Override
	public UnitMachine getModel() {
		return um;
	}
	
}
